package com.tucy.logistics.common.result;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

/**
 * author: tcy
 * createDate: 2022/10/8
 * description:
 */
@Data
public class Result<T> {

    /**
     * 状态编码
     */
    private int code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 响应时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date date;

    /**
     * 成功响应
     */
    public static <T>Result<T> success(T data){
        Result<T> result = new Result<>();
        result.setCode(ResultEnums.SUCCESS.getCode());
        result.setMessage(ResultEnums.SUCCESS.getMsg());
        result.setData(data);
        result.setDate(new Date());
        return result;
    }

    /**
     * 失败响应
     */
    public static <T>Result<T> fail(T data){
        Result<T> result = new Result<>();
        result.setCode(ResultEnums.Fail.getCode());
        result.setMessage(ResultEnums.Fail.getMsg());
        result.setData(data);
        result.setDate(new Date());
        return result;
    }
}
